using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;
/// <summary>
/// Summary description for SpotDAO
/// </summary>
public class SpotDAO:DBAccess
{
	
    #region sql Commands   
    string SQL_SELECT_ALL = "select * from Spot";
    string SQL_SELECT_BY_ID = "select * from Spot where ID=@id";
    string SQL_INSERT = "insert into Spot(Name,CateID,Description,ImageURL,Article,Tag,IsRecommended,ModifiedDate,ModifiedBy,State)values(@Name,@CateID,@Description,@ImageURL,@Article,@Tag,@IsRecommended,@ModifiedDate,@ModifiedBy,@State)";
    string SQL_UPDATE = "update Spot set Name = @Name, CateID = @CateID,Description= @Description,ImageURL=@ImageURL ,Article=@Article,Tag=@Tag,IsRecommended=@IsRecommended,ModifiedDate=@ModifiedDate,ModifiedBy=@ModifiedBy,State=@State where ID = @id";
    string SQL_DELETE = "delete from Spot where ID = @id";
    #endregion

    #region fields
    string message = "";
    #endregion

    #region constructors
    public SpotDAO()
    {

    }
    #endregion

    #region properties
    public string Message
    {
        get { return message; }
        set { message = value; }
    }
    #endregion

    #region Methos
    /// <summary>
    /// Get spot by id
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Spot GetSpot(int id)
    {
        Spot spot = new Spot();
        try
        {

            SqlCommand command = new SqlCommand(SQL_SELECT_BY_ID, conn);
            if (Connect())
            {
                command.Parameters.AddWithValue("@id", id);
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    spot.Id = dr.GetInt32(0);
                    spot.Name = dr.GetString(1);
                    spot.Category.Id= dr.GetInt32(2);
                    spot.Location.Id = dr.GetInt32(3);
                    spot.Description = dr.GetString(4);
                    spot.ImageURL = dr.GetString(5);                   
                    spot.Article = dr.GetString(6);                   
                    spot.Tag = dr.GetString(7);
                    spot.IsRecommended = dr.GetBoolean(8);
                    spot.State = dr.GetBoolean(9);
                    spot.ModifiedDate = dr.GetDateTime(10);
                    spot.ModifiedBy = dr.GetInt32(11);
                    
                }
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return spot;
    }
    /// <summary>
    /// Get all spots 
    /// </summary>
    /// <returns></returns>
    public IList<Spot> GetAllSpots()
    {
        IList<Spot> list = new List<Spot>();
        try
        {
            SqlCommand command = new SqlCommand(SQL_SELECT_ALL, conn);
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    Spot spot = new Spot();
                    spot.Id = dr.GetInt32(0);
                    spot.Name = dr.GetString(1);
                    spot.Category.Id = dr.GetInt32(2);
                    spot.Description = dr.GetString(3);
                    spot.ImageURL = dr.GetString(4);
                    spot.Article = dr.GetString(5);
                    spot.Tag = dr.GetString(6);
                    spot.IsRecommended = dr.GetBoolean(7);
                    spot.ModifiedDate = dr.GetDateTime(8);
                    spot.ModifiedBy = dr.GetInt32(9);
                    spot.State = dr.GetBoolean(10);
                    list.Add(spot);
                }
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return list;
    }
    /// <summary>
    /// Insert new spot into database
    /// </summary>
    /// <param name="tour"></param>
    /// <returns></returns>
    public int Insert(Spot spot)
    {
        int effectedRows = 0;
        try
        {
            SqlCommand command = new SqlCommand(SQL_INSERT, conn);
            command.Parameters.AddWithValue("@Name", spot.Name);
            command.Parameters.AddWithValue("@CateID", spot.Category.Id);           
            command.Parameters.AddWithValue("@Description", spot.Description);
            command.Parameters.AddWithValue("@ImageURL", spot.ImageURL);           
            command.Parameters.AddWithValue("@Article", spot.Article);            
            command.Parameters.AddWithValue("@Tag", spot.Tag);
            command.Parameters.AddWithValue("@IsRecommended", spot.IsRecommended);
            command.Parameters.AddWithValue("@ModifiedDate", spot.ModifiedDate);
            command.Parameters.AddWithValue("@ModifiedBy", spot.ModifiedBy);
            command.Parameters.AddWithValue("@State", spot.State);
            if (Connect())
            {
                effectedRows = command.ExecuteNonQuery();
                command.CommandText = "SELECT @@IDENTITY From Spot";
                spot.Id = int.Parse(command.ExecuteScalar().ToString());
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRows;
    }

    /// <summary>
    /// Update spot
    /// </summary>
    /// <param name="tour"></param>
    /// <returns></returns>
    public int Update(Spot spot)
    {
        int effectedRows = 0;
        try
        {
            SqlCommand command = new SqlCommand(SQL_UPDATE, conn);
            command.Parameters.AddWithValue("@Name", spot.Name);
            command.Parameters.AddWithValue("@CateID", spot.Category.Id);
            command.Parameters.AddWithValue("@Description", spot.Description);
            command.Parameters.AddWithValue("@ImageURL", spot.ImageURL);
            command.Parameters.AddWithValue("@Article", spot.Article);
            command.Parameters.AddWithValue("@Tag", spot.Tag);
            command.Parameters.AddWithValue("@IsRecommended", spot.IsRecommended);
            command.Parameters.AddWithValue("@ModifiedDate", spot.ModifiedDate);
            command.Parameters.AddWithValue("@ModifiedBy", spot.ModifiedBy);
            command.Parameters.AddWithValue("@State", spot.State);
            command.Parameters.AddWithValue("@id",spot.Id);  
            if (Connect())
            {
                effectedRows = command.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRows;
    }

    /// <summary>
    /// Remove spot out of database
    /// </summary>
    /// <param name="tour"></param>
    /// <returns></returns>
    public int Delete(int Id)
    {
        int effectedRows = 0;
        try
        {
            SqlCommand command = new SqlCommand(SQL_DELETE, conn);
            command.Parameters.AddWithValue("@id", Id);
            if (Connect())
            {
                effectedRows = command.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRows;
    }
    /// <summary>
    /// Get spot list with paging
    /// </summary>
    /// <param name="currentPage">current page to view</param>
    /// <param name="pageSize">number of elements per page</param>
    /// <param name="total">total record in query result set (out parameter)</param>
    /// <returns></returns>
    public IList<Spot> GetSpots(int currentPage, int pageSize, ref int total, byte state)
    {
        IList<Spot> list = new List<Spot>();
        try
        {
            SqlCommand command = new SqlCommand("sp_SelectSpots", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@pagesize", SqlDbType.Int);
            command.Parameters.Add("@currentpage", SqlDbType.Int);
            command.Parameters.Add("@state", SqlDbType.TinyInt);
            command.Parameters.Add("@totals", SqlDbType.Int);
            command.Parameters["@pagesize"].Value = pageSize;
            command.Parameters["@currentpage"].Value = currentPage;
            command.Parameters["@state"].Value = state;
            command.Parameters["@totals"].Direction = ParameterDirection.Output;
            command.Parameters["@totals"].Value = total;
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    Spot sp = new Spot();
                    sp.Id = dr.GetInt32(1);
                    sp.Name = dr.GetString(2);
                    sp.Description = dr.GetString(3);
                    sp.ImageURL = dr.GetString(4);
                    sp.State = dr.GetBoolean(5);
                    sp.Category.Name = dr.GetString(6);
                    list.Add(sp);
                }
                dr.Close();
                total = int.Parse(command.Parameters["@totals"].Value.ToString());
            }
        }
        catch (SqlException ex) { }
        catch (Exception ex) { }
        return list;
    }

    /// <summary>
    /// Get spot list with paging and condition
    /// </summary>
    /// <param name="currentPage"></param>
    /// <param name="pageSize"></param>
    /// <param name="total"></param>
    /// <param name="keyword"></param>
    /// <returns></returns>
    public IList<Spot> GetSpotsExt(int currentPage, int pageSize, ref int total, int locationId,string keyword,byte state)
    {
        IList<Spot> list = new List<Spot>();
        try
        {
            SqlCommand command = new SqlCommand("sp_SelectSpotsExt", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@pagesize", SqlDbType.Int);
            command.Parameters.Add("@currentpage", SqlDbType.Int);
            command.Parameters.Add("@locationId", SqlDbType.Int);
            command.Parameters.Add("@keyword", SqlDbType.NVarChar);
            command.Parameters.Add("@state", SqlDbType.TinyInt);
            command.Parameters.Add("@totals", SqlDbType.Int);
            command.Parameters["@pagesize"].Value = pageSize;
            command.Parameters["@currentpage"].Value = currentPage;
            command.Parameters["@locationId"].Value = locationId;
            command.Parameters["@keyword"].Value = keyword;
            command.Parameters["@state"].Value = state;
            command.Parameters["@totals"].Direction = ParameterDirection.Output;
            command.Parameters["@totals"].Value = total;
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    Spot sp = new Spot();
                    sp.Id = dr.GetInt32(1);
                    sp.Name = dr.GetString(2);
                    sp.Description = dr.GetString(3);
                    sp.ImageURL = dr.GetString(4);
                    sp.State = dr.GetBoolean(5);
                    sp.Category.Name = dr.GetString(6);
                    list.Add(sp);
                }
                dr.Close();
                total = int.Parse(command.Parameters["@totals"].Value.ToString());
            }
        }
        catch (SqlException ex) { }
        catch (Exception ex) { }
        return list;
    }

    /// <summary>
    /// Get list of spots belong to a category
    /// </summary>
    /// <param name="currentPage"></param>
    /// <param name="pageSize"></param>
    /// <param name="total"></param>
    /// <param name="cateId">ID of a category</param>
    /// <returns></returns>
    public IList<Spot> GetSpotsByCategory(int currentPage, int pageSize, ref int total, int cateId,byte state)
    {
        IList<Spot> list = new List<Spot>();
        try
        {
            SqlCommand command = new SqlCommand("sp_SelectSpotsByCate", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@pagesize", SqlDbType.Int);
            command.Parameters.Add("@currentpage", SqlDbType.Int);
            command.Parameters.Add("@categoryId", SqlDbType.Int);
            command.Parameters.Add("@state", SqlDbType.TinyInt);
            command.Parameters.Add("@totals", SqlDbType.Int);
            command.Parameters["@pagesize"].Value = pageSize;
            command.Parameters["@currentpage"].Value = currentPage;
            command.Parameters["@categoryId"].Value = cateId;
            command.Parameters["@state"].Value = state;
            command.Parameters["@totals"].Direction = ParameterDirection.Output;
            command.Parameters["@totals"].Value = total;
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    Spot sp = new Spot();
                    sp.Id = dr.GetInt32(1);
                    sp.Name = dr.GetString(2);
                    sp.Description = dr.GetString(3);
                    sp.ImageURL = dr.GetString(4);
                    sp.State = dr.GetBoolean(5);
                    sp.Category.Name = dr.GetString(6);
                    list.Add(sp);
                }
                dr.Close();
                total = int.Parse(command.Parameters["@totals"].Value.ToString());
            }
        }
        catch (SqlException ex) { }
        catch (Exception ex) { }
        return list;
    }
    #endregion
}
